part of '../repository.dart';

const _kLexicalItemAdverbType = 'adverb';

class _AdverbDto extends Equatable implements _WordDto {
  const _AdverbDto._internal({
    required this.id,
    required this.title,
    required this.translation,
    required this.transcriptionDtos,
    required this.useCaseDtos,
    required this.imageId,
    required this.rank,
  });

  @override
  final String id;

  @override
  final String title;

  @override
  final String translation;

  @override
  final BuiltMap<_DialectDto, String> transcriptionDtos;

  @override
  final BuiltList<AssetUseCaseDto> useCaseDtos;

  @override
  final String? imageId;

  @override
  final int rank;

  @override
  String get type => _kLexicalItemAdverbType;

  @override
  List<Object?> get props {
    return [
      id,
      title,
      translation,
      transcriptionDtos,
      useCaseDtos,
      imageId,
      rank,
    ];
  }

  factory _AdverbDto.fromJson(Map<String, dynamic> json) {
    final transcriptionMap = json[_kLexicalItemTranscriptionsKey] as Map;

    final transcriptionDtos = transcriptionMap.map((dialectStringified, transcription) {
      return MapEntry(_DialectDto.fromString(dialectStringified), transcription as String);
    }).build();

    final useCaseMaps = List.castFrom<dynamic, Map>(json[_kLexicalItemUseCasesKey]);

    final useCaseDtos = useCaseMaps.map((useCaseMap) {
      final useCaseJson = Map.castFrom<dynamic, dynamic, String, dynamic>(useCaseMap);
      return AssetUseCaseDto.fromJson(useCaseJson);
    }).toBuiltList();

    return _AdverbDto._internal(
      id: json[_kLexicalItemIdKey],
      title: json[_kLexicalItemTitleKey],
      translation: json[_kLexicalItemTranslationKey],
      transcriptionDtos: transcriptionDtos,
      useCaseDtos: useCaseDtos,
      imageId: json[_kLexicalItemImageIdKey],
      rank: json[_kWordRankKey],
    );
  }

  @override
  AssetAdverb toEntity() {
    final transcriptions = transcriptionDtos.map((dialectDto, transcription) {
      return MapEntry(dialectDto.toEntity(), transcription);
    });

    final useCases = useCaseDtos.map((useCaseDto) {
      return useCaseDto.toAssetUseCase();
    }).toBuiltList();

    return AssetAdverb(
      id: id,
      title: title,
      translation: translation,
      transcriptions: transcriptions,
      useCases: useCases,
      imageId: imageId,
      rank: rank,
    );
  }
}